package com.hayden.fileMonitor;

import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import com.hayden.fileMonitor.util.ConfigFileReader;
import com.hayden.fileMonitor.util.LogbackUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

public class AppStart {
    public static void main(String[] args) {
        LogbackUtil.setLogLevel();
        Logger LOGGER = LoggerFactory.getLogger(AppStart.class);
        LOGGER.info("Service Starting...");
        MonitoringService monitoringService = new MonitoringService();
        ConfigFileReader reader = new ConfigFileReader();
//        InputStream rs = MonitoringService.class.getClassLoader().getResourceAsStream("monitor.properties");
//        Properties pro = new Properties();

        //目录
        String path = reader.getProperty("monitor.folderPath");
        File directory = new File(path);
        LOGGER.info("监控目录为：{}",path);
        //观察者
        FileAlterationObserver observer = new FileAlterationObserver(directory);
        //监听器
        observer.addListener(new MonitoringService.MyFileAlterationListener());
        String time = reader.getProperty("monitor.interval");
        long interval = Long.parseLong(time);//观察频率
        LOGGER.info("观察频率为：{}ms",time);
        //监控器
        FileAlterationMonitor monitor = new FileAlterationMonitor(interval);
        monitor.addObserver(observer);
        try {
            monitor.start();
        } catch (Exception e) {
            LOGGER.error("监控开启失败：",e);
        }
    }
}
